using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

using Impact.Infrastructure.Plugins;
using Impact.Infrastructure;

namespace Impact.Model.Objects
{
    /// <summary>
    /// extensions for the PagePlugin DAO
    /// </summary>
    public partial class PagePlugin : ISerializable
    {
        /// <summary>
        /// Gets the parameter objects associated with this PagePlugin relation
        /// </summary>
        public Dictionary<string, PluginParameter> GetParameters()
        {
            if ( _parameters == null )
            {
                object result = GetColumnValue<byte[]>( "Parameters" );
                if ( result != null )
                {
                    _parameters = (Dictionary<string, PluginParameter>)Utility.Deserialize( result );
                }

                if ( _parameters == null ) // if its still null, or result was null, create an empty dictionary
                {
                    _parameters = NewParametersList( PluginId );
                }
            }
            return _parameters;
        }

        /// <summary>
        /// Sets the parameters associated with this PagePLugin relation
        /// </summary>
        /// <param name="value"></param>
        public void SetParameters(Dictionary<string, PluginParameter> value)
        {
            _parameters = value;

            SetColumnValue( "Parameters", Utility.Serialize( value ) );
        }

        /// <summary>
        /// private internal member for caching the deserialized copy of request data.  so we 
        /// don't have to deserialize it everytime the getter is called
        /// </summary>
        private Dictionary<string, PluginParameter> _parameters;

        /// <summary>
        /// create a new parameters list object.  helper for the GetParameters() method.
        /// </summary>
        private static Dictionary<string, PluginParameter> NewParametersList( Guid pluginId )
        {
            IImpactPlugin plugin = PluginCache.Instance.GetPlugin( pluginId );
            return plugin.Parameters;
        }


        /// <summary>
        /// Gets the plugin object that this PluginPage represents.
        /// </summary>
        public IImpactPlugin Plugin
        {
            get
            {
                IImpactPlugin output = null;
                try
                {
                    output = PluginCache.Instance.GetPlugin( this.PluginId );
                }
                catch ( KeyNotFoundException ex )
                {
                    throw new ArgumentException(
                        string.Format( "Plugin is not configured for this application: {0}", this.PluginId ), ex );
                }
                catch ( Exception ex )
                {
                    throw ex;
                }
                return output;
            }
        }

        /// <summary>
        /// set default values specific to PagePlugins
        /// </summary>
        public new void SetDefaults()
        {
            base.SetDefaults();
            this.Sequence = Int32.MaxValue;
        }



        #region ISerializable Members

        /// <summary>
        /// constructor for serializing this object
        /// </summary>
        public PagePlugin( SerializationInfo info, StreamingContext context )
        {
            this.RegistrationId = info.GetInt32( "RegistrationId" );
            this.PageId = info.GetInt32( "PageId" );
            this.PluginId = (Guid)info.GetValue( "PluginId", typeof( Guid ) );
            this.Location = info.GetInt32( "Location" );
            this.Sequence = info.GetInt32( "Sequence" );
            this.IsNew = info.GetBoolean( "IsNew" );

            Dictionary<string, PluginParameter> parameters = (Dictionary<string, PluginParameter>)info.GetValue( "Parameters", typeof( Dictionary<string, PluginParameter> ) );
            if ( parameters != null )
            {
                //Parameters = parameters;
            }

        }

        /// <summary>
        /// accessor for serializing this object
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        public void GetObjectData( SerializationInfo info, StreamingContext context )
        {
            info.AddValue( "RegistrationId", this.RegistrationId );
            info.AddValue( "PageId", this.PageId );
            info.AddValue( "PluginId", this.PluginId );
            info.AddValue( "Location", this.Location );
            info.AddValue( "Sequence", this.Sequence );
            info.AddValue( "IsNew", this.IsNew );
            info.AddValue( "Parameters", this.Parameters, typeof( Dictionary<string, PluginParameter> ) );
        }

        #endregion


    }
}
